package com.info.dpunified.dpgen.service;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;

public class ExcelUtil {


	public void addNumber(Sheet sheet, int row, int col, Double content)
	{
		 addNumber(sheet, row, col, content, null, false, false, false, false);
	}
	
	public void addString(Sheet sheet, int row, int col, String content)
	{
		addString(sheet, row, col, content, null, false, false, false, false);
	}
	
	public void addString(Sheet sheet, int row, int col, String content, CellStyle cellStyle,boolean top, boolean right, boolean bottom, boolean left)
	{
		Row sheetRow = sheet.getRow(row - 1);
		if(sheetRow == null)
		{
			sheetRow = sheet.createRow(row - 1);
		}
		if(bottom)
		{
			cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
			cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		}
		if(left)
		{
			cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
			cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		}
		if(right)
		{
			cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
			cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		}
		if(top)
		{
			cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
			cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		}
		Cell cell = sheetRow.createCell((short) col - 1);
		cell.setCellValue(content);
	    if(cellStyle != null)
		cell.setCellStyle(cellStyle);
	    //CellUtil.setAlignment(cell, wb, CellStyle.ALIGN_CENTER);
	}
	public void addNumber(Sheet sheet, int row, int col, Double content, CellStyle cellStyle,boolean top, boolean right, boolean bottom, boolean left)
	{
		Row sheetRow = sheet.getRow(row - 1);
		if(sheetRow == null)
		{
			sheetRow = sheet.createRow(row - 1);
		}
		if(bottom)
		{
			cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
			cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		}
		if(left)
		{
			cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
			cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		}
		if(right)
		{
			cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
			cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		}
		if(top)
		{
			cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
			cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		}
		Cell cell = sheetRow.createCell((short) col - 1);
		cell.setCellValue(content.doubleValue());
		if(cellStyle != null)
		cell.setCellStyle(cellStyle);
	    //CellUtil.setAlignment(cell, wb, CellStyle.ALIGN_CENTER);
	}
	
/*	public void addString(Sheet sheet, Row sheetRow, int row, int col, int torow, int tocol, String content, CellStyle cellStyle)
	{
		Cell cell = sheetRow.createCell((short) col - 1);
		cell.setCellValue(content);
	    cell.setCellStyle(cellStyle);
	    //CellUtil.setAlignment(cell, wb, CellStyle.ALIGN_CENTER);
		sheet.addMergedRegion(new CellRangeAddress(
				row - 1, //first row (0-based)
		            torow -1, //last row  (0-based)
		            col - 1, //first column (0-based)
		            tocol - 1  //last column  (0-based)
		    ));
	}*/
	
	public void addString(Sheet sheet, int row, int col, int torow, int tocol, String content, CellStyle cellStyle, boolean top, boolean right, boolean bottom, boolean left)
	{
		Row sheetRow = sheet.getRow(row - 1);
		if(sheetRow == null)
		{
			sheetRow = sheet.createRow(row - 1);
		}
		
		if(bottom)
		{
			cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
			cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		}
		if(left)
		{
			cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
			cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		}
		if(right)
		{
			cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
			cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		}
		if(top)
		{
			cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
			cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		}
		Cell cell = sheetRow.createCell((short) col - 1);
		cell.setCellValue(content);
	    cell.setCellStyle(cellStyle);
	    
	    for(int iCount = col; iCount < tocol; iCount++)
	    {
	    	cell = sheetRow.createCell((short) iCount);
		    cell.setCellStyle(cellStyle);
	    }
	    
	    //CellUtil.setAlignment(cell, wb, CellStyle.ALIGN_CENTER);
		sheet.addMergedRegion(new CellRangeAddress(
				row - 1, //first row (0-based)
		            torow -1, //last row  (0-based)
		            col - 1, //first column (0-based)
		            tocol - 1  //last column  (0-based)
		    ));
		
		
		
	}
}